<?php
/**
 * 商品搜索索引重建
 *
 * 命令：
 * bash$ ./cli reindex
 *
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 */
class CLI_Reindex extends CLI
{

    public function main()
    {
        if (CLI::choice("\n确认要重建商品搜索索引吗?") === 'y')
        {
            CLI::output("\n旧的商品搜索索引已清除...\n");
            $select =  DB::select('goods_id')->from('goods')->where('deleted', '=', 0);
            $iterator = new Db_Iterator($select, NULL, 50);
            $count = $iterator->count();
            $index = 1;
            CLI::output("\n共 ".$iterator->total()." 个商品，正在重建商品搜索索引...\n");
            $start_time = microtime(true);
            while ($iterator->valid())
            {
                CLI::output("\n[".str_pad($index, strlen($count) + 2, ' ', STR_PAD_BOTH)."/ $count ]  ".str_pad(round($index/$count*100, 2), 5, ' ', STR_PAD_LEFT)." % ");
                foreach ($iterator->current() as $row)
                {
                    Goods_Index::update($row['goods_id']);
                    CLI::output(">");
                }
                $iterator->next();
                $index++;
            }
            CLI::output("\n\n商品搜索索引重建完成，共耗时 ".number_format(microtime(true)-$start_time, 8, '.', '')." 秒\n");
        }
    }

}